{template "header.html"}
<div class="note note-danger">
    <p>升级程序之前，请务必备份全站数据</p>
</div>

<div class="right-card-box">
<form class="form-horizontal" role="form" id="myform">
    {dr_form_hidden()}
    <div class="table-scrollable">
        <table class="table table-striped table-bordered table-hover table-checkable dataTable">
            <thead>
            <tr class="heading">
                {if !$is_oem}<th class="myselect"></th>{/if}
                <th width="80" style="text-align: center"> 类型</th>
                <th width="250"> 程序名称</th>
                <th width="170"> 更新时间 </th>
                <th width="100"> 版本 </th>
                <th width="110" style="text-align: center"> 备份 </th>
                <th><a onclick="dr_update_version_all()"  class="badge badge-danger badge-roundless"> {dr_lang('检测全部')} </a> </th>
            </tr>
            </thead>
            <tbody>
            {php $i=1;$dirs=[];}
            {loop $list $dir $t}
            <tr class="odd gradeX">
                {php $dirs[$t.id]=$dir;}
                {if !$is_oem}<td class="myselect">
                {if $t.type == 'app'}
                    <label class="mt-table mt-checkbox mt-checkbox-single mt-checkbox-outline">
                        <input type="checkbox" class="checkboxes" name="ids[]" value="{$t.id}" />
                        <span></span>
                    </label>{/if}
                </td>{/if}
                <td style="text-align: center">{$t.tname}</td>
                <td>{$t.name}{if $t.type == 'app'} / {$dir}{/if}</td>
                <td> {$t.updatetime} </td>
                <td>{if !$is_oem}<a href="javascript:dr_show_log('{$t.id}', '{$t.version}');">{$t.version}</a>{else}{$t.version}{/if}</td>
                <td  style="text-align: center">
					{if $t.backup}
                    <a href="javascript:dr_show_info('<p>文件备份时间：{$t.backup_time}</p><p>程序备份目录：{$t.backup}</p><p>模板备份目录：{if $t.backup_tpl}{$t.backup_tpl}{else}未备份{/if}</p>');" class="label label-success"> 已备份 </a>
                    {else}
                    <span class="label label-danger"> 未备份 </span>
                    {/if}
                </td>
                <td>
                    <label style="display: none" id="dr_update_{$dir}">
                        <button type="button" onclick="dr_update_cms('{dr_url('cloud/todo_update', ['id'=>$t.id, 'dir'=>$dir])}', '{dr_lang('升级前请做好系统备份，你确定要升级吗？')}', 1)" class="btn red btn-xs"> <i class="fa fa-cloud-upload"></i> {dr_lang('在线升级')}</button>
                        {if !$is_oem}
                            {if $dir == 'phpcmf'}
                            <a href="https://www.xunruicms.com/member.php?action=down&cid={$cmf_version.id}&is_update=v{$t.version}&vs={$vs}" target="_blank" class="btn green btn-xs"> <i class="fa fa-cloud-download"></i> {dr_lang('离线下载升级包')}</a>
                            {else}
                            <a href="javascript:dr_down_cms('{$t.id}');" class="btn green btn-xs"> <i class="fa fa-cloud-download"></i> {dr_lang('离线下载升级包')}</a>
                            {/if}
                        {/if}
					</label>
                    <label style="display: none" id="dr_duibi_{$dir}">
                        <a href="https://www.xunruicms.com/member.php?action=down&cid={$cmf_version.id}&is_update=v{$t.version}&vs={$vs}" target="_blank" class="btn green btn-xs"> <i class="fa fa-code"></i> {dr_lang('离线下载升级包')}</a>
                        {if IS_DEV}
                        <button type="button" onclick="dr_update_cms('{dr_url('cloud/todo_update', ['id'=>$t.id, 'dir'=>$dir])}', '{dr_lang('更新前请做好系统备份，你确定要更新吗？')}', 1)" class="btn red btn-xs"> <i class="fa fa-cloud-upload"></i> {dr_lang('在线更新')}</button>
                        {/if}
                    </label>
                    <label class="dr_check_version" id="dr_row_{$dir}">
                        <a onclick="dr_update_version('{$dir}', '{$t.id}', '{$t.version}')"  class="badge badge-danger badge-roundless"> {dr_lang('检测版本')} </a>
                    </label>
                </td>
            </tr>
            {php $i++;}
            {/loop}

            </tbody>
        </table>
    </div>
    {if !$is_oem}
    <div class="row fc-list-footer table-checkable ">
        <div class="col-md-12 fc-list-select">
            <label class="mt-table mt-checkbox mt-checkbox-single mt-checkbox-outline">
                <input type="checkbox" class="group-checkable" data-set=".checkboxes" />
                <span></span>
            </label>
            <label style="margin-left: 15px;"><button type="button" onclick="dr_update_all()" class="btn blue btn-sm"> <i class="fa fa-cloud-download"></i> {dr_lang('批量在线升级')}</button></label>
            <label><button type="button" onclick="dr_down_all()" class="btn green btn-sm"> <i class="fa fa-cloud-download"></i>  {dr_lang('批量离线下载')}</button></label>

        </div>
    </div>
    {/if}
</form>
</div>
<script type="text/javascript">

    function dr_down_cms(id) {
        var login_url = '{dr_url("cloud/login")}';
        layer.open({
            type: 2,
            title: '登录官方云账号',
            fix:true,
            scrollbar: false,
            shadeClose: true,
            shade: 0,
            area: ['500px', '260px'],
            btn: [lang['ok'], lang['esc']],
            yes: function(index, layero){
                var body = layer.getChildFrame('body', index);
                $(body).find('.form-group').removeClass('has-error');
                // 延迟加载
                var loading = layer.load(2, {
                    shade: [0.3,'#fff'], //0.1透明度的白色背景
                    time: 100000000
                });
                $.ajax({type: "POST",dataType:"json", url: login_url, data: $(body).find('#myform').serialize(),
                    success: function(json) {
                        layer.close(loading);
                        if (json.code == 1) {
                            layer.close(index);
                            var yz_url = '{dr_url("cloud/down_app")}&id='+id+'&vs={$vs}&ls='+json.data;
                            location.href= yz_url;
                        } else {
                            $(body).find('#dr_row_'+json.data.field).addClass('has-error');
                            dr_tips(0, json.msg);
                        }
                        return false;
                    },
                    error: function(HttpRequest, ajaxOptions, thrownError) {
                        dr_ajax_alert_error(HttpRequest, this, thrownError);
                    }
                });
                return false;
            },
            content: login_url+'&is_iframe=1'
        });
    }


    function dr_down_all() {
        var ids = new Array();
        var idss = '';
        var i = 0;
        $(".checkboxes:checked").each(function(){
            ids[i] = this.value;
            idss+= ','+this.value;
            i++;
        });
        if (ids.length < 1) {
            dr_tips(0, '你还没有选中需要下载的选项');
            return false;
        }
        dr_down_cms(idss);
    }

    function dr_update_all() {
        var dirs_array = {json_encode($dirs)};
        var ids = new Array();
        var dirs = '';
        var idss = '';
        var i = 0;
        $(".checkboxes:checked").each(function(){
            ids[i] = this.value;
            idss+= ','+this.value;
            dirs+= ','+dirs_array[this.value];
            i++;
        });
        if (ids.length < 1) {
            dr_tips(0, '你还没有选中需要升级的选项');
            return false;
        }
        var url = "{dr_url('cloud/todo_update')}&id="+idss+"&vs={$vs}&dir="+dirs;
        layer.confirm(
            "升级前请做好系统备份，你确定要升级吗？",
            {
                icon: 3,
                shade: 0,
                title: lang['ts'],
                btn: ['直接升级','备份再升级', lang['esc']]
            }, function(index, layero){
                layer.close(index);
                dr_todo_cms(url+'&is_bf=1');
            }, function(index){
                layer.close(index);
                dr_todo_cms(url+'&is_bf=0');
            });
    }

    function dr_update_cms(url, msg, remove) {
        layer.confirm(
        msg,
        {
            icon: 3,
            shade: 0,
            title: lang['ts'],
            btn: ['直接升级','备份再升级', lang['esc']]
        }, function(index, layero){
            layer.close(index);
            dr_todo_cms(url+'&is_bf=1');
        }, function(index){
            layer.close(index);
            dr_todo_cms(url+'&is_bf=0');
        });
    }

    function dr_todo_cms(url) {
        var login_url = '{dr_url("cloud/login")}';
        layer.open({
            type: 2,
            title: '登录官方云账号',
            fix:true,
            scrollbar: false,
            shadeClose: true,
            shade: 0,
            area: ['500px', '260px'],
            btn: [lang['ok'], lang['esc']],
            yes: function(index, layero){
                var body = layer.getChildFrame('body', index);
                $(body).find('.form-group').removeClass('has-error');
                // 延迟加载
                var loading = layer.load(2, {
                    shade: [0.3,'#fff'], //0.1透明度的白色背景
                    time: 100000000
                });
                $.ajax({type: "POST",dataType:"json", url: login_url, data: $(body).find('#myform').serialize(),
                    success: function(json) {
                        layer.close(loading);
                        if (json.code == 1) {
                            layer.close(index);
                            var yz_url = url+'&'+$('#myform').serialize()+'&ls='+json.data;
                            // 验证成功
                            layer.open({
                                type: 2,
                                title: '升级程序',
                                scrollbar: false,
                                resize: true,
                                maxmin: true, //开启最大化最小化按钮
                                shade: 0,
                                area: ['80%', '80%'],
                                success: function(layero, index){
                                    // 主要用于后台权限验证
                                    dr_iframe_error(layero, index, 0);
                                },
                                content: yz_url
                            });
                        } else {
                            $(body).find('#dr_row_'+json.data.field).addClass('has-error');
                            dr_tips(0, json.msg);
                        }
                        return false;
                    },
                    error: function(HttpRequest, ajaxOptions, thrownError) {
                        dr_ajax_alert_error(HttpRequest, this, thrownError);
                    }
                });
                return false;
            },
            content: login_url+'&is_iframe=1'
        });
    }

    function dr_beifen_cms(url, msg, remove) {
        layer.confirm(
                msg,
                {
                    icon: 3,
                    shade: 0,
                    title: lang['ts'],
                    btn: [lang['ok'], lang['esc']]
                }, function(index){
                    layer.close(index);
                    layer.open({
                        type: 2,
                        title: '备份程序',
                        scrollbar: false,
                        resize: true,
                        maxmin: true, //开启最大化最小化按钮
                        shade: 0,
                        area: ['80%', '80%'],
                        success: function(layero, index){
                            // 主要用于后台权限验证
                            dr_iframe_error(layero, index, 0);
                        },
                        content: url
                    });
                });
    }
    
    function dr_show_log(id, v) {
        layer.open({
            type: 2,
            title: '版本日志',
            scrollbar: false,
            resize: true,
            maxmin: true, //开启最大化最小化按钮
            shade: 0,
            area: ['80%', '80%'],
            content: '{dr_url("cloud/log_show")}&id='+id+'&version='+v,
            //content: 'http://www.phpcmf.net/version.php?id='+id+'&version='+v,
        });
    }


    function dr_update_version_all() {
        {loop $list $dir $t}
        {if $t.id}
            dr_update_version("{$dir}", "{$t.id}", "{$t.version}");
        {/if}
        {/loop}
    }

    function dr_update_version(dir, id, version) {
        $("#dr_row_"+dir).html("<img style='height:17px' src='{THEME_PATH}assets/images/loading-0.gif'>");
        $("#dr_update_"+dir).hide();
        $.ajax({
            type: "GET",
            dataType: "json",
            url: "{dr_url('cloud/check_version')}&id="+id+"&version="+version,
            success: function(json) {
                if (json.code == 1) {
                    $("#dr_row_"+dir).html(json.msg);
                    $("#dr_update_"+dir).show();
                } else if (json.code == 2) {
                    $("#dr_row_"+dir).html(json.msg);
                    $("#dr_duibi_"+dir).show();
                } else {
                    $("#dr_row_"+dir).html("<a onclick=\"dr_update_version('"+dir+"', '"+id+"', '"+version+"')\"  class=\"badge badge-danger badge-roundless\"> {dr_lang('检测版本')} </a>");
                    $("#dr_row_"+dir).append("&nbsp;&nbsp;<font color='red'>"+json.msg+"</font>");
                }
            },
            error: function(HttpRequest, ajaxOptions, thrownError) {
                $("#dr_row_"+dir).html("<a onclick=\"dr_update_version('"+dir+"', '"+id+"', '"+version+"')\"  class=\"badge badge-danger badge-roundless\"> {dr_lang('检测版本')} </a>");
                $("#dr_row_"+dir).append("&nbsp;&nbsp;<font color='red'>网络异常，请稍后再试</font>");
            }
        });
    }

</script>


{template "footer.html"}